package com.dc.project_kotlin.adapter

import android.content.Context
import android.text.TextUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.dc.base.view.recyclerview.CommonRecyclerAdapter
import com.dc.base.view.recyclerview.MulitiTypeSupport
import com.dc.base.view.recyclerview.ViewHolder
import com.dc.project_kotlin.R
import com.dc.project_kotlin.bean.ArticleResponse
import com.dc.project_kotlin.ext.toHtml
import com.dc.project_kotlin.view.CollectView

class ArticleAdapter(context: Context, data: MutableList<ArticleResponse>) :
    CommonRecyclerAdapter<ArticleResponse>(
        context,
        data,
        object : MulitiTypeSupport<ArticleResponse> {
            override fun getLayoutId(t: ArticleResponse): Int {
                if (TextUtils.isEmpty(t.envelopePic)) {
                    return R.layout.item_ariticle
                }
                return R.layout.item_project
            }

        }) {

    private var showTag = false

    constructor(context: Context, data: MutableList<ArticleResponse>, showTag: Boolean) :this(context, data) {
        this.showTag = showTag
    }

    override fun convert(holder: ViewHolder, t: ArticleResponse, position: Int) {
        if (TextUtils.isEmpty(t.envelopePic)) {
            //文章布局的赋值
            t.run {
                holder.setText(
                    R.id.item_home_author,
                    if (author.isNotEmpty()) author else shareUser
                )
                holder.setText(R.id.item_home_content, title.toHtml())
                holder.setText(R.id.item_home_type2, "$superChapterName·$chapterName".toHtml())
                holder.setText(R.id.item_home_date, niceDate)
                holder.getView<CollectView>(R.id.item_home_collect).isChecked = collect
                if (showTag) {
                    //展示标签
                    holder.setViewVisibility(R.id.item_home_new, fresh)
                    holder.setViewVisibility(R.id.item_home_top, type == 1)
                    if (tags.isNotEmpty()) {
                        holder.setViewVisibility(R.id.item_home_type1, true)
                        holder.setText(R.id.item_home_type1, tags[0].name)
                    } else {
                        holder.setViewVisibility(R.id.item_home_type1, false)
                    }
                } else {
                    //隐藏所有标签
                    holder.setViewVisibility(R.id.item_home_top, false)
                    holder.setViewVisibility(R.id.item_home_type1, false)
                    holder.setViewVisibility(R.id.item_home_new, false)
                }
            }
            holder.getView<CollectView>(R.id.item_home_collect)
                .setOnCollectViewClickListener(object : CollectView.OnCollectViewClickListener {
                    override fun onClick(v: CollectView) {
                        collectAction.invoke(t, v, holder.adapterPosition)
                    }
                })
        } else {
            //项目布局的赋值
            t.run {
                holder.setText(
                    R.id.item_project_author,
                    if (author.isNotEmpty()) author else shareUser
                )
                holder.setText(R.id.item_project_title, title.toHtml())
                holder.setText(R.id.item_project_content, desc.toHtml())
                holder.setText(
                    R.id.item_project_type,
                    "$superChapterName·$chapterName".toHtml()
                )
                holder.setText(R.id.item_project_date, niceDate)
                if (showTag) {
                    //展示标签
                    holder.setViewVisibility(R.id.item_project_new, fresh)
                    holder.setViewVisibility(R.id.item_project_top, type == 1)
                    if (tags.isNotEmpty()) {
                        holder.setViewVisibility(R.id.item_project_type1, true)
                        holder.setText(R.id.item_project_type1, tags[0].name)
                    } else {
                        holder.setViewVisibility(R.id.item_project_type1, false)
                    }
                } else {
                    //隐藏所有标签
                    holder.setViewVisibility(R.id.item_project_top, false)
                    holder.setViewVisibility(R.id.item_project_type1, false)
                    holder.setViewVisibility(R.id.item_project_new, false)
                }
                holder.getView<CollectView>(R.id.item_project_collect).isChecked = collect
                Glide.with(mContext).load(envelopePic)
                    .transition(DrawableTransitionOptions.withCrossFade(500))
                    .into(holder.getView(R.id.item_project_imageview))
            }
            holder.getView<CollectView>(R.id.item_project_collect)
                .setOnCollectViewClickListener(object : CollectView.OnCollectViewClickListener {
                    override fun onClick(v: CollectView) {
                        collectAction.invoke(t, v, holder.adapterPosition)
                    }
                })
        }
    }

    private var collectAction: (item: ArticleResponse, v: CollectView, position: Int) -> Unit = {item, v, position ->}

    fun setCollectClick(action: (item: ArticleResponse, v: CollectView, position: Int) -> Unit) {
        this.collectAction = action
    }
}